package com.ruoyi.server.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.server.domain.ServerRegion;
import com.ruoyi.server.domain.dto.ServerInfoAddDTO;
import com.ruoyi.server.domain.dto.ServerInfoPageDTO;
import com.ruoyi.server.domain.dto.ServerInfoUpdateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.server.mapper.ServerInfoMapper;
import com.ruoyi.server.domain.ServerInfo;
import com.ruoyi.server.service.IServerInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 服务器Service业务层处理
 *
 * @author ghostmen
 * @date 2024-10-31
 */
@Service
public class ServerInfoServiceImpl extends ServiceImpl<ServerInfoMapper, ServerInfo> implements IServerInfoService {
    @Autowired
    private ServerInfoMapper serverInfoMapper;

    /**
     * 查询服务器
     *
     * @param serverId 服务器主键
     * @return 服务器
     */
    @Override
    public ServerInfo selectServerInfoByServerId(Long serverId) {
        return serverInfoMapper.selectServerInfoByServerId(serverId);
    }

    /**
     * pageDTO
     *
     * @param pageDTO 服务器
     * @return 服务器
     */
    @Override
    public List<ServerInfo> selectServerInfoList(ServerInfoPageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return serverInfoMapper.selectServerInfoList(pageDTO);
    }

    @Override
    public List<ServerInfo> getServerList() {
        return serverInfoMapper.getServerList();
    }

    /**
     * 新增服务器
     *
     * @param addDTO 服务器
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertServerInfo(ServerInfoAddDTO addDTO) {
        //判断是否存在
        Long count = serverInfoMapper.selectCount(new QueryWrapper<ServerInfo>().lambda()
                .eq(ServerInfo::getServer, addDTO.getServer())
        );
        if (count > 0) {
            throw new ServiceException("该服务器《" + addDTO.getServer() + "》已存在，请勿重复添加");
        }
        ServerInfo serverInfo = BeanUtil.copyProperties(addDTO, ServerInfo.class);
        serverInfo.setServerId(IdWorker.getId());
        serverInfo.setCreateBy(addDTO.getLoginUser().getUsername());
        serverInfo.setCreateTime(DateUtils.getNowDate());
        return serverInfoMapper.insertServerInfo(serverInfo);
    }

    /**
     * 修改服务器
     *
     * @param updateDTO 服务器
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateServerInfo(ServerInfoUpdateDTO updateDTO) {
        //判断是否存在
        Long count = serverInfoMapper.selectCount(new QueryWrapper<ServerInfo>().lambda()
                .eq(ServerInfo::getServer, updateDTO.getServer())
                .ne(ServerInfo::getServerId, updateDTO.getServerId())
        );
        if (count > 0) {
            throw new ServiceException("该服务器《" + updateDTO.getServer() + "》已存在，请勿重复添加");
        }
        ServerInfo serverInfo = BeanUtil.copyProperties(updateDTO, ServerInfo.class);
        serverInfo.setUpdateBy(updateDTO.getLoginUser().getUsername());
        serverInfo.setUpdateTime(DateUtils.getNowDate());
        return serverInfoMapper.updateServerInfo(serverInfo);
    }

    /**
     * 批量删除服务器
     *
     * @param serverIds 需要删除的服务器主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteServerInfoByServerIds(Long[] serverIds) {
        return serverInfoMapper.deleteServerInfoByServerIds(serverIds);
    }

    /**
     * 删除服务器信息
     *
     * @param serverId 服务器主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteServerInfoByServerId(Long serverId) {
        return serverInfoMapper.deleteServerInfoByServerId(serverId);
    }
}
